package com.kitty.game.task.service.taskHandler;

import com.kitty.common.model.Reason;

import com.kitty.common.utils.DecodeSendMessage;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.service.other.YaBiaoBossHandler;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.drop.newservice.NewDropService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.game.utils.Const;
import com.kitty.game.utils.DateTimeUtil;
import com.kitty.game.utils.DateUtils;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.LoginEvent;

import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Date;

/**人物飞升处理类*/
@Component
public class YaBiaoTaskHandler extends TaskHandler {
    Logger logger = LoggerFactory.getLogger(getClass());
    /**人物飞升任务的任务id*/
    private int taskId = 96;
    private int taskId2 = 2500;
    private int taskId3 = 2501;
    /**结婴需要金钱*/
    private int reqMoney = 1000000;
    /**结婴需要元宝*/
    private int reqGold = 100000;

    private static final int ACCEPT_MIN_LEVEL = 75;

    private static final String LEVEL_NOT_ENOUGH = "你修行尚浅，还是不要冒险行事了(等级不足#R{0}级#n)";

    private static final String ACCEPTED = "你已经领取过押镖任务了，赶快去完成吧。";

    @Autowired
    TeamService teamService;

    @Autowired
    RoleService roleService;

    @Override
    public TaskType getTaskType() {
        return TaskType.YABIAO;
    }

    @EventHandler(EventType.LOGIN)
    public void handleLoginEvent(LoginEvent loginEvent) {
        Role role = loginEvent.getRole();
        if (role.getTask().isAccepted(2500)) {
            TaskSet taskSet = taskService.getTaskSet(2500, role);
//            if (!isDeleteTask(role, taskSet)) {return ;}
            deleteTask(role,taskSet);
        }
        if (role.getTask().isAccepted(2501)) {
            TaskSet taskSet2 = taskService.getTaskSet(2501, role);
//            if (!isDeleteTask(role, taskSet)) {return ;}
            deleteTask(role,taskSet2);
        }
        if (role.getTask().isAccepted(2502)) {
            TaskSet taskSet3 = taskService.getTaskSet(2502, role);
            deleteTask(role,taskSet3);
        }
//        if(taskSet.getTaskName().equals("押送镖银")){
//            if (isDeleteTask(role, taskSet) == false) {return ;}
//            continue;
//        }
    }

    public boolean isInTime(){
//        String aaaa = DateUtils.dayForWeek(new Date());
//        if (aaaa.equals("1") ||  aaaa.equals("3") || aaaa.equals("5") ){
//            return true;
//        }
        return true;
    }
    /**
     * [押镖开始]
     * @param role
     * @param msg
     * @param pnc
     */
    public void yabiao_start(Role role, NPC pnc, String msg) {
        String open = DataCache.REDIS_DATA.get("yabiao_status");
        if(!"1".equals(open)){
            MessagePusher.pushMessage(role, new RespMsg("押镖活动还未开始，请关注公告。"));
            return ;
        }

        /**等级不足*/
        int lv = Integer.parseInt(DataCache.REDIS_DATA.get("yabiao_lv"));
        if (role.getLevel() < lv) {
//            String msg1 = MessageFormat.format(LEVEL_NOT_ENOUGH, ACCEPT_MIN_LEVEL);
////            String msg1 = MessageFormat.format(LEVEL_NOT_ENOUGH, role.getName(), ACCEPT_MIN_LEVEL);
//            MessagePusher.pushMessage(role, new RespMsg(msg1));
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("你的等级不足 #Y" + lv + "级#n 不能进行押镖"));
            return ;
        }

        if(role.getActivity().getYaBiaoCount() >=  Integer.parseInt(DataCache.REDIS_DATA.get("YABIAO_COUNT"))){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("押镖活动今天已做满#R"+role.getActivity().getYaBiaoCount()+"#n次，请明天再来吧"));
            return;
        }

//        ActivityService activityService = SpringUtils.getActivityService();
//        int remainCount = activityService.getRemainCount(role, ActivityType.XUAN_SHANG);
//        /**剩余次数没有了*/
//        if (remainCount <= 0) {
//            String msg = REMAIN_COUNT_NOT_ENOUGH;
//            MessagePusher.pushMessage(role, new RespMsg(msg));
//            return "";
//        }

        /**已经接取了*/
        if (role.getTask().isAccepted(taskId2)) {
            String msg2 = ACCEPTED;
            MessagePusher.pushMessage(role, new RespMsg(msg2));
            return ;
        }
        Team team = teamService.getTeam(role.getRoleId());
        if(team != null){
            String msg2 = "押镖不允许组队!!";
            MessagePusher.pushMessage(role, new RespMsg(msg2));
            return ;
        }

//        if (isCanFetchReward(role)) {
//            return FIRST_FETCH_CONTENT;
//        }

        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId2, role);
        acceptTask(role, taskSet);


//        for(Member member : team.getList()){
//            Role role1 = roleService.getOnlinePlayer(member.getRoleId());
//
//        }
        roleService.subtractMoney(role,100000, Reason.YABIAO);
        //SpringUtils.getRoleService().sendExtraAttribute(role);
        SpringUtils.getBroadcastService().sendUpdateAppear(role);

        MessagePusher.pushMessage(role, new RespMsg("成功领取任务，点击任务会自动寻路！"));
    }

    //除掉强盗
    public void taskStep_yabiaostep2(Role role,String msg, NPC npc) {
        SpringUtils.getBean(YaBiaoBossHandler.class).flushBoss(role);
    }
    //除掉土匪
    public void taskStep_yabiaostep3(Role role,String msg, NPC npc) {
        SpringUtils.getBean(YaBiaoBossHandler.class).flushBoss_tf(role);
    }
    //除掉土匪
    public void taskStep_yabiaostep4(Role role,String msg, NPC npc) {

        MessagePusher.pushMessage(role,new RespMsg("本轮押镖任务完成,请找#R杨镖头#n领取新的押镖任务"));
        TaskSet taskSet = taskService.getTaskSet(2500, role);
        TaskSet taskSet2 = taskService.getTaskSet(2501, role);
        TaskSet taskSet3 = taskService.getTaskSet(2502, role);
//            if (!isDeleteTask(role, taskSet)) {return ;}
        deleteTask(role,taskSet);
        deleteTask(role,taskSet2);
        deleteTask(role,taskSet3);

        if(role.getActivity().getYaBiaoCount() >=  Integer.parseInt(DataCache.REDIS_DATA.get("YABIAO_COUNT"))){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("押镖活动今天已进行#R"+role.getActivity().getYaBiaoCount()+"#n次，不再获得奖励"));
            return;
        }
        String reward = DataCache.REDIS_DATA.get("yabiao_jiangli");
        for (String s : reward.split("\\+")) {
            SpringUtils.getBean(NewDropService.class).addRewardTask(role,s,"");
        }

        //roleService.addMoney(role1,3000000,Reason.YABIAO);
        role.getActivity().setYaBiaoCount(role.getActivity().getYaBiaoCount() + 1);

        SpringUtils.getBroadcastService().sendUpdateAppear(role);

    }

    /**检测资源*/
    private boolean checkResource(Role role) {
        if (role.getMoney() < reqMoney && role.getGold() < reqGold) {
            MessagePusher.notify2Player(role, I18nId.PMT_2301);
            return false;
        }
        return true;
    }

    /**扣除资源*/
    private void substractResource(Role role) {
        SpringUtils.getRoleService().subtractGold(role,100000000,Reason.ROLE_JIEYING);
//        if (role.getMoney() >= reqMoney) {
//            SpringUtils.getRoleService().subtractMoney(role, reqMoney, Reason.ROLE_JIEYING);
//            return ;
//        }
//        if (role.getGold() >= reqGold) {
//            SpringUtils.getRoleService().subtractGold(role, reqGold, Reason.ROLE_JIEYING);
//            return ;
//        }
    }
}
